
package com.UI;

import com.Model.NotesDbAdapter;


import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class NotepadEdit extends Activity {

	private EditText mTitleText;
    private EditText mBodyText;
    private Long mRowId;
    private NotesDbAdapter mDbHelper;
    private String smsBodyText;
    private String smsTitleText;
    private String cNo;  
    private String emailBodyText;
    private String emailTitleText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDbHelper = new NotesDbAdapter(this);
        mDbHelper.open();
        
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.note_edit);
    	getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.notes_edittitlebar);
                      
        mTitleText = (EditText) findViewById(R.id.title);
        mBodyText = (EditText) findViewById(R.id.body);
      
        //retrieve course number from last activity
        Bundle extras = getIntent().getExtras();
        cNo = extras.getString("CourseNum");
        
        ((TextView)findViewById(R.id.titleNotes)).setText("Course Notes " + cNo);
        
    	findViewById(R.id.back).setOnClickListener(new OnClickListener() {
    		public void onClick(View v) {
    			NotepadEdit.this.finish();

    		}
    	});
               
        Button confirmButton = (Button) findViewById(R.id.confirm);
        
        if(savedInstanceState != null){
        	mRowId=savedInstanceState.getLong(NotesDbAdapter.KEY_ROWID);
        
        }
        else{
        	mRowId=null;
        }

		if (mRowId == null) { 
			
			if(extras.getLong(NotesDbAdapter.KEY_ROWID) != 0){
				mRowId=extras.getLong(NotesDbAdapter.KEY_ROWID);
				cNo=extras.getString("CourseNum");
			}
		    else {
			    mRowId=null;
		    }
		}

		populateFields();
		
        confirmButton.setOnClickListener(new View.OnClickListener() {

        	public void onClick(View view) {
        	    setResult(RESULT_OK);
        	    finish();
        	}         
        });
        
		Button btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
		btnSendSMS.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View component) {
        	    setResult(RESULT_OK);
        	    finish();
		        Intent sendIntent = new Intent(Intent.ACTION_VIEW);
		        smsTitleText = mTitleText.getText().toString();
		        smsBodyText = mBodyText.getText().toString(); 
		        smsBodyText = "Title: " + smsTitleText + "\nNote: " + smsBodyText; 
		        sendIntent.putExtra("sms_body", smsBodyText); 
		        sendIntent.setType("vnd.android-dir/mms-sms");
		        startActivity(sendIntent);
			}
		});
		
		
		// Send Email will not work on the Emulator. Works on a real device
		// where it gives various email application list that can perform this task.
		Button btnSendEmail = (Button) findViewById(R.id.btnSendEmail);
		btnSendEmail.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View component) {
				finish();
				Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
				emailTitleText = " " + mTitleText.getText().toString();
				emailBodyText = " " + mBodyText.getText().toString(); 
		        
				emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, emailTitleText);
				emailIntent.setType("plain/text");
				emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailBodyText);
				startActivity(Intent.createChooser(emailIntent, "Send your email in:"));		
			}
		});
    }
    
	    // display title and body of note in the editor activity
	    private void populateFields() {
	        if (mRowId != null) {
	            Cursor note = mDbHelper.fetchNote(mRowId);
	            startManagingCursor(note);
	            mTitleText.setText(note.getString(
	    	            note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
	            mBodyText.setText(note.getString(
	                    note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
	        }
	    }
	    	    
	    @Override
	    protected void onSaveInstanceState(Bundle outState) {
	        super.onSaveInstanceState(outState);
	        outState.putLong(NotesDbAdapter.KEY_ROWID, mRowId);	        	        
	    }
	    
	    @Override
	    protected void onPause() {
	        super.onPause();
	        Intent i = new Intent(this, NotepadAddDelete.class);
	        i.putExtra("CourseNum",cNo);
	        saveState();
	    }
	    
	    @Override
	    protected void onResume() {
	        super.onResume();
	        populateFields();
	    }
	    
	    
	    private void saveState() {
	        String title = mTitleText.getText().toString();
	        String body = mBodyText.getText().toString();

	        if (mRowId == null) {
	            long id = mDbHelper.createNote(title, body, cNo);
	            if (id > 0) {
	                mRowId = id;
	            }
	        } else {
	            mDbHelper.updateNote(mRowId, title, body,cNo);
	        }
	    }
	    
}
